﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Question1
{
    class Program
    {
        static void Main(string[] args)
        {
            GenericList<int> genericList = new GenericList<int>();
            genericList.Add(1);
            genericList.Add(3);
            genericList.Add(5);
            Console.Write("遍历所有元素：");
            genericList.ForEach(m => Console.Write(m + " "));
            Console.WriteLine();
            int max = genericList.Head.Data;
            genericList.ForEach(m => {
                if (m > max)
                {
                    max = m;
                }
            });
            Console.WriteLine("最大值："+max);
            int min = genericList.Head.Data;
            genericList.ForEach(m => {
                if (m < min)
                {
                    min = m;
                }
            });
            Console.WriteLine("最小值：" + min);
            int sum = 0;
            genericList.ForEach(m => {
                sum += m;
            });
            Console.WriteLine("求和：" + sum);
        }
    }
    public class Node<T>
    {
        public Node<T> Next { get; set; }
        public T Data { get; set; }
        public Node(T t)
        {
            Next = null;
            Data = t;
        }
    }

    public class GenericList<T>
    {
        private Node<T> head;
        private Node<T> tail;

        public GenericList()
        {
            tail = head = null;
        }

        public Node<T> Head { get => head; }

        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                head = tail = n;
            }
            else
            {
                tail.Next = n;
                tail = n;
            }
        }

        public void ForEach(Action<T> action)
        {
            for(Node<T> n = head; n != null; n = n.Next)
            {
                action(n.Data);
            }
        }
    }
}
